﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace PiControls.Controls.myTabControl
{
     public class  CustomTabControl : TabControl
    {



        private Color m_PrimaryColor = Color.White;

        private Color m_SecondaryColor = Color.Black;

        public void setPrimaryColor(Color color)
        {
            m_PrimaryColor = color;
        }

        public void setSecondaryColor(Color color)
        {
            m_SecondaryColor = color;
        }
        public CustomTabControl()
        {
            // 启用自绘模式
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            // 调整选项卡大小
            this.ItemSize = new Size(120, 30);
            // 去除默认选项卡外边框
            this.SizeMode = TabSizeMode.Fixed;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {

            //获取当前选项卡
            TabPage tabPage = this.TabPages[e.Index];
            Rectangle tabRect = this.GetTabRect(e.Index);

            // 设置背景色
            Brush backBrush = e.Index == this.SelectedIndex ? new SolidBrush(m_SecondaryColor) : new SolidBrush(m_PrimaryColor);
            e.Graphics.FillRectangle(backBrush, tabRect);

            // 设置文本颜色和样式
            Brush textBrush = new SolidBrush(Color.White);
            Font tabFont = new Font("微软雅黑", 10, e.Index == this.SelectedIndex ? FontStyle.Bold : FontStyle.Regular);
            StringFormat stringFormat = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };

            // 绘制文本
            e.Graphics.DrawString(tabPage.Text, tabFont, textBrush, tabRect, stringFormat);

            // 绘制边框（可选）
            e.Graphics.DrawRectangle(Pens.Black, tabRect);

            // 释放资源
            backBrush.Dispose();
            textBrush.Dispose();
            tabFont.Dispose();

        }

    }
}
